package com.shijunhao.graduation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.annotations.OperationLog;
import com.shijunhao.graduation.domain.bean.ResultCode;
import com.shijunhao.graduation.domain.dto.BusinessNoteQueryDto;
import com.shijunhao.graduation.domain.dto.BusinessPostDto;
import com.shijunhao.graduation.domain.entity.BusinessNote;
import com.shijunhao.graduation.domain.vo.BusinessNoteListVo;
import com.shijunhao.graduation.domain.vo.BusinessNoteVo;
import com.shijunhao.graduation.domain.vo.SimpleNoteInfoVo;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.properties.OperationTarget;
import com.shijunhao.graduation.properties.OperationType;
import com.shijunhao.graduation.service.business.IBusinessNoteService;
import com.shijunhao.graduation.service.business.IBusinessPostService;
import com.shijunhao.graduation.service.scheduled.LooksScheduledService;
import com.shijunhao.graduation.utils.PageUtils;
import com.shijunhao.graduation.utils.WrapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: shijunhao
 * @date: 2022/5/22 21:31
 * @description:
 */
@RestController
@RequestMapping("/business-post")
@Api(tags = "问贴管理")
public class BusinessPostController {

    @Resource
    private IBusinessPostService businessPostService;

    @Resource
    private LooksScheduledService looksScheduledService;

    @Resource
    private IBusinessNoteService businessNoteService;

    @GetMapping("/admin/list")
    @ApiOperation("列表查询")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResultCode<IPage<BusinessNote>> adminPage(@ModelAttribute BusinessNoteQueryDto query) {
        query.setType(0);
        Page<BusinessNote> page = businessPostService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(page);
    }

    @PostMapping("insert")
    @ApiOperation("新增")
    @OperationLog(target = OperationTarget.POST,type = OperationType.INSERT,description = "新增问答")
    public ResultCode<Long> insert(@RequestBody BusinessPostDto dto) {
        Long insert = businessPostService.insert(dto);
        if (insert != null)
            return ResultCode.success("插入成功", insert);
        else
            return ResultCode.error("插入失败");
    }


    @PostMapping("delete/{id}")
    @ApiOperation("删除")
    @OperationLog(target = OperationTarget.POST,type = OperationType.DELETE,description = "删除问答")
    public ResultCode<Boolean> delete(@PathVariable Long id) {
        boolean delete = businessNoteService.delete(id);
        if (delete)
            return ResultCode.success("删除成功");
        else
            return ResultCode.error("删除失败");
    }


    @GetMapping("info/{id}")
    @ApiOperation("详情")
    public ResultCode<BusinessNoteVo> info(@PathVariable Long id) {
        BusinessNote info = businessNoteService.getById(id);
        if (info != null && info.getType() == 0) {
            looksScheduledService.userLook(TableName.BUSINESS_NOTE.getLabel(), id);
            BusinessNoteVo mapping = businessNoteService.mapping(info);
            return ResultCode.success(mapping);
        } else {
            return ResultCode.error("没有找到这条信息。");
        }
    }

    @GetMapping("note/info/{id}")
    @ApiOperation("简要详情")
    public ResultCode<SimpleNoteInfoVo> noteInfo(@PathVariable Long id) {
        BusinessNote info = businessNoteService.getById(id);
        if (info != null) {
            return ResultCode.success(businessNoteService.toSimpleInfo(info));
        } else {
            return ResultCode.error("没有找到这条信息。");
        }
    }

    @GetMapping("/list")
    @ApiOperation("简表列表查询")
    public ResultCode<IPage<BusinessNoteListVo>> page(@ModelAttribute BusinessNoteQueryDto query) {
        query.setType(0);
        Page<BusinessNote> page = businessNoteService.page(PageUtils.getPage(), WrapperUtils.query(query));
        return ResultCode.success(page.convert(businessNoteService::listVoMapping));
    }

    @GetMapping("/list/new")
    @ApiOperation("最新问答简表")
    public ResultCode<IPage<BusinessNoteListVo>> pageNew() {
        return ResultCode.success(businessNoteService.pageNew(PageUtils.getPage()));
    }

    @GetMapping("/list/mine")
    @ApiOperation("我的问答列表")
    public ResultCode<IPage<BusinessNoteListVo>> pageMine() {
        return ResultCode.success(businessNoteService.pageMine(PageUtils.getPage(), Wrappers.lambdaQuery(BusinessNote.class).eq(BusinessNote::getType,0)));
    }


    @GetMapping("/suggest/{id}")
    @ApiOperation("相关推荐")
    public ResultCode<IPage<BusinessNoteListVo>> pageSuggest(@PathVariable Long id) {
        return ResultCode.success(businessNoteService.pageSuggest(id));
    }
}
